<?php

namespace App\Console\Commands;

use App\Accounts;
use App\RemoteAccountsV3;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class DataV3HasModify extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'DataV3HasModify';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';



    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 修改过的个数
     * @var int
     */
    private $modify_num = 0;

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(0);

        // 全表扫描处理   将 已推送过来，确被修改了的；去除已推送的标记
        RemoteAccountsV3::where('push_id','=',1)->select(['id','appid','name'])
            ->chunk(5000,function ($datas){
            if ($datas){
                $addAccounts = $datas->toArray();
                foreach ($addAccounts as $row){
                    // 查询有没有
                    $id = Accounts::where(['appid' => $row['appid'],'name' => $row['name']])->value('id');
                    if (!$id){
                        $one = DB::connection("mysql_47_v3")->table('meta_accounts')
                            ->where('id', '=', $row['id'])
                            ->update(['push_id' => 0]);

                        if ($one){
                            $this->modify_num++;
                        }
                    }
                }
            }
        });


        echo "v3 中已修改了".$this->modify_num."条源数据";
    }
}
